package com.tuanzi.loan.business.repository.settlement.impl;

import static com.tuanzi.loan.generated.jooq.loan.Tables.LOAN_COMPANY_INFO;
import static com.tuanzi.loan.generated.jooq.loan.Tables.LOAN_PRODUCT_INFO;

import java.util.List;

import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.tuanzi.loan.business.entity.settlement.CompanyInfo;
import com.tuanzi.loan.business.repository.settlement.custom.CompanyInfoRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.settlement.CompanyInfoSearchRequest;
import com.tuanzi.loan.vo.response.settlement.CompanyInfoSearchResponse;

/**
 * @author caiwanlin
 * @create 2018-01-12 9:58
 */
public class CompanyInfoRepositoryImpl extends JOOQBaseRepositoryPlus implements CompanyInfoRepositoryCustom {
    @Override
    protected TableImpl getTable() {
        return LOAN_COMPANY_INFO;
    }

    @Override
    public Page<CompanyInfoSearchResponse> list(CompanyInfoSearchRequest searchRequest, Pageable pageable) {
        fields(LOAN_COMPANY_INFO.ID,
                LOAN_COMPANY_INFO.COMPANY_NAME,
                LOAN_COMPANY_INFO.CREATOR,
                LOAN_COMPANY_INFO.CREATE_TIME,
                LOAN_COMPANY_INFO.PRINCIPAL,
                LOAN_COMPANY_INFO.AUDIT_STATUS,
                LOAN_PRODUCT_INFO.NAME.as("productName"));
        leftJoin(LOAN_PRODUCT_INFO,LOAN_PRODUCT_INFO.COMPANY_ID.eq(LOAN_COMPANY_INFO.ID));
        between(LOAN_COMPANY_INFO.CREATE_TIME,searchRequest.getStartTime(),searchRequest.getEndTime());
        eq(LOAN_COMPANY_INFO.PRINCIPAL,searchRequest.getPrincipal());
        contains(LOAN_COMPANY_INFO.COMPANY_NAME,searchRequest.getCompanyName());
        groupBy(LOAN_COMPANY_INFO.ID);

        return search(pageable,new CompanyInfoSearchResponseMapper());
    }

    @Override
    public List<CompanyInfo> findAllCompanyName() {
        fields(LOAN_COMPANY_INFO.ID,
                LOAN_COMPANY_INFO.COMPANY_NAME);
        //只查找审核通过的公司
        eq(LOAN_COMPANY_INFO.AUDIT_STATUS,4);
        return search(CompanyInfo.class);
    }

    private final class CompanyInfoSearchResponseMapper implements RecordMapper<Record,CompanyInfoSearchResponse>{

        @Override
        public CompanyInfoSearchResponse map(Record record) {
            CompanyInfoSearchResponse response = new CompanyInfoSearchResponse();
            response.setId(record.get(LOAN_COMPANY_INFO.ID));
            response.setCompanyName(record.get(LOAN_COMPANY_INFO.COMPANY_NAME));
            response.setCreator(record.get(LOAN_COMPANY_INFO.CREATOR));
            response.setCreateTime(record.get(LOAN_COMPANY_INFO.CREATE_TIME));
            response.setPrincipal(record.get(LOAN_COMPANY_INFO.PRINCIPAL));
            response.setAuditStatus(record.get(LOAN_COMPANY_INFO.AUDIT_STATUS));
            response.setProductName(record.get("productName",String.class));
            return response;
        }
    }

    private final class CompanyInfoMapper implements RecordMapper<Record,CompanyInfo>{

        @Override
        public CompanyInfo map(Record record) {
            CompanyInfo companyInfo = new CompanyInfo();
            companyInfo.setId(record.get(LOAN_COMPANY_INFO.ID));
            companyInfo.setCompanyName(record.get(LOAN_COMPANY_INFO.COMPANY_NAME));
            return companyInfo;
        }
    }



}
